import Vuex from 'vuex'

import Cookieparser from 'cookieparser';


const store = () => new Vuex.Store({
  state: {
    data1: [],
    token: null
  },
  mutations: {
    SET_LIST: (state, data) => {
      state.data1 = data
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    }
  },
  actions: {
    nuxtServerInit ({ commit }, { req }) {
      let accessToken = null;
      if (req.headers.cookie) {
        let parsed = Cookieparser.parse(req.headers.cookie);
        if (parsed.accessToken) {
          accessToken = parsed.accessToken;
        }
      }
      commit('SET_TOKEN', accessToken)
    }
  }
});

export default store
